package com.casework


import org.apache.commons.lang.builder.HashCodeBuilder
import com.casework.auth.Role
import com.casework.auth.AuthUser

class LegalCaseFirm implements Serializable{
    LegalCase legalCase
    Firm firm
    
    boolean equals(other) {
		if (!(other instanceof LegalCaseFirm)) {
			return false
		}

		other.legalCase?.id == legalCase?.id &&
			other.firm?.id == firm?.id
	}

	int hashCode() {
		def builder = new HashCodeBuilder()
		if (legalCase) builder.append(legalCase.id)
		if (firm) builder.append(firm.id)
		builder.toHashCode()
	}
    
    static LegalCaseFirm get(long legalCaseId, long firmId) {
		find 'from LegalCaseFirm where legalCase.id=:legalCaseId and firm.id=:firmId',
			[legalCaseId: legalCaseId, firmId: firmId]
	}

	static LegalCaseFirm create(LegalCase legalCase, Firm firm, boolean flush = false) {
		new LegalCaseFirm(legalCase: legalCase, firm: firm).save(flush: flush, insert: true)
	}

	static boolean remove(AuthUser legalCase, Role firm, boolean flush = false) {
		LegalCaseFirm instance = LegalCaseFirm.findByLegalCaseAndFullDescription(legalCase, firm)
		instance ? instance.delete(flush: flush) : false
	}

	static mapping = {
		id composite: ['firm', 'legalCase']
		version false
	}
}
